﻿/// <reference path="scripts/typings/leaflet/leaflet.d.ts" />
/// <reference path="scripts/typings/jquerymobile/jquerymobile.d.ts" />
/// <reference path="Scripts/typings/jqueryui/jqueryui.d.ts" />
/// <reference path="Scripts/typings/jquery/jquery.d.ts" />
/// <reference path="application/mapping.ts" />
/// <reference path="Scripts/typings/google.geolocation/google.geolocation.d.ts" />
/// <reference path="scripts/typings/pubsubjs/pubsub.d.ts" />
/// <reference path="scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../mapparola.core.client/mapparolacoreclient.d.ts" />
/// <reference path="../mapparola.core/mapparolacore.d.ts" />
/// <reference path="monkify.ts" />
/// <reference path="scripts/typings/moment/moment.d.ts" />


module Mapparola.Client {

    export class App {

        private monkify: Monkify;

        public ActivePage: View;
        public CurrentUser: Core.User;
        public Panel: JQuery;

        public HeaderView: HeaderView;
        public LoginView: LoginView;
        public LiveView: LiveView;
        public DiscussionView: DiscussionView;


        public Mapping: Mapparola.Client.Mapping;
        public RestDataStore: Mapparola.Core.Client.RestDataStore;


        constructor(element: HTMLElement) {

            this.monkify = new Monkify();



            //Do math on resize
            $(window).on("resize", () => {
                this.ScaleContentToDevice();
            });

            this.Panel = $(".panel");
            this.ScaleContentToDevice();

            this.Mapping = new Mapparola.Client.Mapping(this);
            this.RestDataStore = new Mapparola.Core.Client.RestDataStore();

            //create views, bindings, etc...
            this.HeaderView = new Mapparola.Client.HeaderView(this);
            this.DiscussionView = new Mapparola.Client.DiscussionView(this);


            //Pages
            this.LoginView = new Mapparola.Client.LoginView(this);
            this.LiveView = new Mapparola.Client.LiveView(this);
            //---

            PubSub.subscribe("UserRetrieved", (msg: string, user: Core.User) => {

                this.CurrentUser = user;

            });

            //Request current user
            this.LoginView.GetCurrentAccount();

        }

        public CreateGUID(): string {

            function s4() {
                return Math.floor((1 + Math.random()) * 0x10000)
                    .toString(16)
                    .substring(1);
            }

            return (function () {
                return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
                    s4() + '-' + s4() + s4() + s4();
            })();

        }

        public OpenPanel(): void {

            (<any>this.Panel).panel("open");
        }

        public ClosePanel(): void {

            (<any>this.Panel).panel("close");
        }

        private ScaleContentToDevice() {
            scroll(0, 0);
            var content = $(window).outerHeight() - $(".ui-header").outerHeight() - $(".ui-footer").outerHeight();

            $(".ui-content").height(content);
        }

        public ActivateView(viewEl: HTMLElement): void {

            $(viewEl).switchClass("inactive", "active");
        }

        public DeactivateView(viewEl: HTMLElement): void {

            $(viewEl).switchClass("active", "inactive");
        }

        public ActivatePage(page: Page): void {

            var view: View;

            switch (page) {

                case Page.Login:
                    view = this.LoginView;
                    break;

                case Page.Live:
                    view = this.LiveView;
                    break;

            }

            if (view) {
                view.ActivatePage();
            }
        }
    }

    export enum Page {

        Login = 0,
        Live = 1
    }

}

window.onload = () => {
    var el = document.getElementById('content');
    var app = new Mapparola.Client.App(el);

    (<any>window).mapparola = app;
    (<any>window).mapparolaPurge = () => {
        (<any>window).mapparola.RestDataStore.DeleteAllMessages();
    };

    //app.start();


};